import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

/// 标签具体子项，配置属性可参考 [BrnDeleteTag]
/// ignore: must_be_immutable
class YtDeleteTagItemWidget extends StatelessWidget {
  final int index;
  final String title;
  final Function(int) didDeleted;
  final Size? deleteIconSize;
  final TextStyle? style;
  final OutlinedBorder? shape;
  final Color? backgroundColor;
  final Color? deleteIconColor;
  final bool isLimitWidth; //是否限定宽度
  BrnTagConfig? themeData;

  /// 标签的字体颜色
  final TextStyle? tagTextStyle;

  YtDeleteTagItemWidget(this.index, this.title, this.didDeleted,
      {Key? key,
      this.isLimitWidth = true,
      this.deleteIconSize,
      this.style,
      this.shape,
      this.backgroundColor,
      this.deleteIconColor,
      this.tagTextStyle,
      this.themeData})
      : super(key: key) {
    themeData ??= BrnTagConfig();
    themeData = BrnThemeConfigurator.instance
        .getConfig(configId: themeData!.configId)
        .tagConfig
        .merge(this.themeData);
    themeData = themeData!.merge(BrnTagConfig(
        tagBackgroundColor: this.backgroundColor,
        tagTextStyle: BrnTextStyle.withStyle(tagTextStyle)));
  }
  @override
  Widget build(BuildContext context) {
    return isLimitWidth
        ? Container(
            width: themeData!.tagWidth + 7,
            constraints: BoxConstraints(
                maxWidth: themeData!.tagWidth + 7,
                minWidth: themeData!.tagMinWidth + 7,
                maxHeight: themeData!.tagHeight),
            alignment: Alignment.topCenter,
            margin: EdgeInsets.only(bottom: 6, top: 6),
            child: Chip(
              padding: EdgeInsets.fromLTRB(8, 0, 0, 3),
              labelPadding: EdgeInsets.fromLTRB(0, 4, -3, 4),
              backgroundColor: themeData!.tagBackgroundColor,
              label: Text(title,
                  overflow: TextOverflow.ellipsis,
                  style: themeData!.tagTextStyle.generateTextStyle()),

              shape: shape ??
                  RoundedRectangleBorder(
                      borderRadius:
                          BorderRadius.circular(themeData!.tagRadius)),
              //删除图标
              deleteIcon: deleteIconSize != null
                  ? BrunoTools.getAssetSizeImage(BrnAsset.iconClose,
                      deleteIconSize!.width, deleteIconSize!.height,
                      color: deleteIconColor)
                  : BrunoTools.getAssetImageWithColor(
                      BrnAsset.iconClose, deleteIconColor),
              onDeleted: () {
                debugPrint('$index');
                didDeleted(index);
              },
            ))
        : Container(
            margin: EdgeInsets.only(bottom: 6, top: 6),
            child: Chip(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 3),
              labelPadding: EdgeInsets.fromLTRB(0, 4, -3, 4),
              backgroundColor: themeData!.tagBackgroundColor,
              label: Text(title,
                  overflow: TextOverflow.ellipsis,
                  style: themeData!.tagTextStyle.generateTextStyle()),

              shape: shape ??
                  RoundedRectangleBorder(
                      borderRadius:
                          BorderRadius.circular(themeData!.tagRadius)),
              //删除图标
              deleteIcon: deleteIconSize != null
                  ? BrunoTools.getAssetSizeImage(BrnAsset.iconClose,
                      deleteIconSize!.width, deleteIconSize!.height,
                      color: deleteIconColor)
                  : BrunoTools.getAssetImageWithColor(
                      BrnAsset.iconClose, deleteIconColor),
              onDeleted: () {
                debugPrint('$index');
                didDeleted(index);
              },
            ));
  }
}

/// 标签控制器，用于主动添加和删除标签
class YtBrnDeleteTagController {
  late ValueNotifier<List<String>> notifier;

  /// 控制器中存储的标签数据
  List<String> _tags = [];

  List<String> get tags => notifier.value;

  YtBrnDeleteTagController({List<String>? initTags}) {
    _tags = initTags ?? [];
    notifier = ValueNotifier(_tags);
  }

  /// 初始时设置全量的标签
  void setTags(List<String> tags) {
    this._tags = tags;
    _asyncData();
  }

  /// 添加标签集合
  void addTags(List<String> tags) {
    _tags.addAll(tags);
    _asyncData();
  }

  /// 添加单个标签到集合末尾
  void addTag(String tag) {
    _tags.add(tag);
    _asyncData();
  }

  /// 清空所有标签
  void clear() {
    _tags.clear();
    _asyncData();
  }

  /// 删除指定 index 的标签，并返回其内容
  String? deleteForIndex(int index) {
    if (_tags.length > index) {
      String result = _tags.removeAt(index);
      _asyncData();
      return result;
    } else {
      return null;
    }
  }

  /// 删除某个具体内容的标签，成功返回 true，失败返回 false
  bool deleteForTag(String tag) {
    bool result = _tags.remove(tag);
    _asyncData();
    return result;
  }

  void _asyncData() {
    // notifier 中的 value 引用是 _tags 所以直接赋值 _tags 不会触发回调
    List<String> values = [];
    _tags.forEach((e) => values.add(e));
    notifier.value = values;
  }
}
